package com.qclix.inv;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Invoice implements Comparable<Invoice>, Serializable {
	private static final long serialVersionUID = 1L;
	
    private String number;
    private Date issueDate;
    private Client client;
    private List<InvoiceLine> lines;
    private Contract contract;

    public Contract getContract() {
        return contract;
    }

    public void setContract(Contract contract) {
        this.contract = contract;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String value) {
        this.number = value;
    }

    public Date getIssueDate() {
        return issueDate;
    }

    public void setIssueDate(Date value) {
        this.issueDate = value;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client value) {
        this.client = value;
    }

    public List<InvoiceLine> getLines() {
        if (lines == null) {
            lines = new ArrayList<InvoiceLine>();
        }
        return this.lines;
    }

    public void setLines(List<InvoiceLine> lines) {
        this.lines = lines;
    }

    public int compareTo(Invoice o) {
        if (o == null)
            return 1;
        int dateComparison = this.issueDate.compareTo(o.issueDate);
        if (dateComparison != 0)
            return dateComparison;
        return this.number.compareTo(o.number);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Invoice invoice = (Invoice) o;

        if (client != null ? !client.equals(invoice.client) : invoice.client != null) return false;
        if (contract != null ? !contract.equals(invoice.client) : invoice.contract != null) return false;
        if (issueDate != null ? !issueDate.equals(invoice.issueDate) : invoice.issueDate != null) return false;
        if (lines != null ? !lines.equals(invoice.lines) : invoice.lines != null) return false;
        if (number != null ? !number.equals(invoice.number) : invoice.number != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = number != null ? number.hashCode() : 0;
        result = 31 * result + (issueDate != null ? issueDate.hashCode() : 0);
        result = 31 * result + (client != null ? client.hashCode() : 0);
        result = 31 * result + (contract != null ? contract.hashCode() : 0);
        result = 31 * result + (lines != null ? lines.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Invoice [");
        builder.append("number = ").append(getNumber());
        builder.append(", date = ").append(new SimpleDateFormat("dd/MM/yyy").format(getIssueDate()));
        if (getClient() != null) {
            builder.append(", client = " + getClient().getName());
        } else if (getContract() != null) {
            builder.append(", contract = " + getContract().getReference());
        }
        builder.append(", nb of lines = " + getLines().size());
        builder.append("]");
        return builder.toString();
    }
}
